//
//  TTShopManagerSetViewController.m
//  TaoTuo
//
//  Created by admin on 2019/11/8.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTShopManagerSetViewController.h"
#import "TTGeneralTableViewCell.h"


static NSString *cellID = @"cell";

@interface TTShopManagerSetViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    LLGeneralButtonCellModel *openAllCellModel;
    LLGeneralButtonCellModel *openFinishedCellModel;
    LLGeneralButtonCellModel *openMineCellModel;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) TTDianpuManagerModel *managerModel;
@property (nonatomic, strong) NSMutableDictionary *paramsDic;

@property (nonatomic, assign) BOOL hasChangeSet;  //设置改变了

@end

@implementation TTShopManagerSetViewController

- (instancetype)initWithManagerModel:(TTDianpuManagerModel *)managerModel
{
    self = [super init];
    if (self) {
        self.managerModel = managerModel;
    }
    return self;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    if (self.hasChangeSet) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreashDatas" object:nil];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"商品管理";
    [self setupDatas];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *array = self.dataArray[section];
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *array = self.dataArray[indexPath.section];
    TTGeneralTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = array[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLGeneralCellModel *model = self.dataArray[indexPath.section][indexPath.row];
    if ([model isKindOfClass:[LLGeneralButtonCellModel class]]) {
        LLGeneralButtonCellModel *btnModel = (LLGeneralButtonCellModel *)model;
        if (btnModel.clickBlock) {
            btnModel.clickBlock(btnModel, btnModel.button);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
    view.backgroundColor = UIColorHex(F1F5F6);
    return view;

}

// 上传结果
- (void)uploadSetResultWithSuccessBlock:(dispatch_block_t)successBlock failure:(nullable dispatch_block_t)failure
{
    [[LLNetworkManagerTool sharedManager] postUrlString:setShopGoods_api parameters:self.paramsDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (successBlock) {
            successBlock();
        }
        self.hasChangeSet = YES;
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        if (failure) {
            failure();
        }
        
    } showLoadingInView:self.view];
}

- (void)setupDatas
{
    [self.dataArray removeAllObjects];
    
    [self.paramsDic setObject:self.managerModel.ShopProductSourceType forKey:@"param.shopProductSourceType"];
    NSString *openStr = self.managerModel.ShopDiamondsIsOpenQuery ? @"true" : @"false";
    [self.paramsDic setObject:openStr forKey:@"param.shopDiamondsIsOpenQuery"];
    NSString *topStr = self.managerModel.ShopCollectionSortTop ? @"true" : @"false";
    [self.paramsDic setObject:topStr forKey:@"param.shopCollectionSortTop"];
    
    NSMutableArray *array1 = [NSMutableArray array];
    //0 所有商品 1 收藏商品  2 仅显示成品
    @weakify(self)
    openAllCellModel = [LLGeneralButtonCellModel setModelWithImageName:nil title:@"开放全部商品" contentAttributedString:nil rightNormalImageName:@"shopcart_unsel" rightSelectedImageName:@"shopcartlist_sel" clickBlock:^(LLGeneralButtonCellModel * _Nullable model, UIButton * _Nullable button) {
        @strongify(self)
        self.paramsDic[@"param.shopProductSourceType"] = @"0";
        @weakify(self)
        [self uploadSetResultWithSuccessBlock:^{
            @strongify(self)
            self->openFinishedCellModel.isSelect = NO;
            self->openMineCellModel.isSelect = NO;
            model.isSelect = YES;
            [self.tableView reloadData];
        } failure:nil];
        
    } isSelect:self.managerModel.ShopProductSourceType.intValue == 0];
    openFinishedCellModel = [LLGeneralButtonCellModel setModelWithImageName:nil title:@"仅开放成品商品" contentAttributedString:nil rightNormalImageName:@"shopcart_unsel" rightSelectedImageName:@"shopcartlist_sel" clickBlock:^(LLGeneralButtonCellModel * _Nullable model, UIButton * _Nullable button) {
        @strongify(self)
        self.paramsDic[@"param.shopProductSourceType"] = @"2";
        @weakify(self)
        [self uploadSetResultWithSuccessBlock:^{
            @strongify(self)
            self->openAllCellModel.isSelect = NO;
            self->openMineCellModel.isSelect = NO;
            model.isSelect = YES;
            [self.tableView reloadData];
        } failure:nil];
        
       } isSelect:self.managerModel.ShopProductSourceType.intValue == 2];
    openMineCellModel = [LLGeneralButtonCellModel setModelWithImageName:nil title:@"仅开放我的收藏商品" contentAttributedString:nil rightNormalImageName:@"shopcart_unsel" rightSelectedImageName:@"shopcartlist_sel" clickBlock:^(LLGeneralButtonCellModel * _Nullable model, UIButton * _Nullable button) {
        @strongify(self)
        self.paramsDic[@"param.shopProductSourceType"] = @"1";
        @weakify(self)
        [self uploadSetResultWithSuccessBlock:^{
            @strongify(self)
            self->openAllCellModel.isSelect = NO;
            self->openFinishedCellModel.isSelect = NO;
            model.isSelect = YES;
            [self.tableView reloadData];
        } failure:nil];
        
       } isSelect:self.managerModel.ShopProductSourceType.intValue == 1];
    
    [array1 addObject:openAllCellModel];
    [array1 addObject:openFinishedCellModel];
    [array1 addObject:openMineCellModel];
    [self.dataArray addObject:array1];
    
    
    NSMutableArray *array2 = [NSMutableArray array];
    [array2 addObject:[LLGeneralSwitchCellModel setModelWithImageName:nil title:@"商城置顶我的收藏商品" contentAttributedString:nil switchBlock:^(LLGeneralSwitchCellModel *switchCellModel, UISwitch *mySwitch) {
        @strongify(self)
        NSString *topStr = mySwitch.on ? @"true" : @"false";
        [self.paramsDic setObject:topStr forKey:@"param.shopCollectionSortTop"];
        [self uploadSetResultWithSuccessBlock:^{
            switchCellModel.isOn = mySwitch.on;
        } failure:^{
            mySwitch.on = !mySwitch.on;
        }];
        
    } isOn:self.managerModel.ShopCollectionSortTop]];
    [array2 addObject:[LLGeneralSwitchCellModel setModelWithImageName:nil title:@"开放裸钻查询" contentAttributedString:nil switchBlock:^(LLGeneralSwitchCellModel *switchCellModel, UISwitch *mySwitch) {
        @strongify(self)
        NSString *openStr = mySwitch.on ? @"true" : @"false";
        [self.paramsDic setObject:openStr forKey:@"param.shopDiamondsIsOpenQuery"];
        [self uploadSetResultWithSuccessBlock:^{
            switchCellModel.isOn = mySwitch.on;
        } failure:^{
            mySwitch.on = !mySwitch.on;
        }];
    } isOn:self.managerModel.ShopDiamondsIsOpenQuery]];
    
    [self.dataArray addObject:array2];
    
    
    [self.tableView reloadData];
    
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColorHex(F1F5F6);
        _tableView.tableFooterView = [UIView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 50;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.separatorColor = kSeparatorColor;
        [_tableView registerClass:[TTGeneralTableViewCell class] forCellReuseIdentifier:cellID];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableDictionary *)paramsDic
{
    if (!_paramsDic) {
        _paramsDic = [NSMutableDictionary dictionary];
    }
    return _paramsDic;
}


@end
